package com.trinity.specialpay.controller;

import com.trinity.common.annotation.Log;
import com.trinity.common.core.controller.BaseController;
import com.trinity.common.core.domain.AjaxResult;
import com.trinity.common.core.page.TableDataInfo;
import com.trinity.common.enums.BusinessType;
import com.trinity.common.utils.SecurityUtils;
import com.trinity.common.utils.poi.ExcelUtil;
import com.trinity.orders.domain.XcOrders;
import com.trinity.orders.service.IXcOrdersService;
import com.trinity.specialpay.domain.CampusActivity;
import com.trinity.specialpay.domain.CampusActivityStudent;
import com.trinity.specialpay.dto.QueryActivityParamsDto;
import com.trinity.specialpay.dto.QueryActivityResultDto;
import com.trinity.specialpay.service.ICampusActivityService;
import com.trinity.specialpay.service.ICampusActivityStudentService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 缴费项目Controller
 *
 * @author trinity
 * @date 2023-11-19
 */
@RestController
@RequestMapping("/campus/activity")
public class CampusActivityController extends BaseController {
    @Resource
    private ICampusActivityService campusActivityService;
    @Resource
    private ICampusActivityStudentService campusActivityStudentService;

    @Resource
    private IXcOrdersService ordersService;

    /**
     * 查询缴费项目列表
     */
    @PreAuthorize("@ss.hasPermi('campus:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(CampusActivity campusActivity) {
        startPage();
        List<CampusActivity> list = campusActivityService.selectCampusActivityList(campusActivity);
        return getDataTable(list);
    }

    /**
     * 查询缴费项目列表
     */
    @PreAuthorize("@ss.hasPermi('campus:activity:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll(CampusActivity campusActivity) {
        List<CampusActivity> list = campusActivityService.selectCampusActivityList(campusActivity);
        return success(list);
    }

    /**
     * 查询缴费项目列表
     */
    @PreAuthorize("@ss.hasPermi('campus:activity:list')")
    @GetMapping("/myAct")
    public AjaxResult myAct(QueryActivityParamsDto queryActivityParamsDto) {
        Long userId = SecurityUtils.getUserId();
        List<CampusActivityStudent> list = campusActivityStudentService.selectCampusActivityStudentList(queryActivityParamsDto);
        List<QueryActivityResultDto> resultDtos = new ArrayList<>(list.size());
        for (CampusActivityStudent ac : list) {
            QueryActivityResultDto vo = new QueryActivityResultDto();
            CampusActivity activity = ac.getActivity();
//            vo.setId(activity.getActId());
            vo.setPayTo(activity.getPayee());
            vo.setName(activity.getActName());
//            vo.setAmount(Double.parseDouble(String.valueOf(activity.getTotalPrice())));
            vo.setExplain(activity.getRemark());
            vo.setStartTime(activity.getCreateTime());
            XcOrders orders = ordersService.getOrderByBusinessId(String.valueOf(ac.getPayId()));
            if (orders == null || "NOTPAY".equals(orders.getStatus())) {
                //未支付
//                vo.setPayTime();
                vo.setPay(false);
            }
            if (orders != null && "SUCCESS".equals(orders.getStatus())) {
                //已
                vo.setPayTime(orders.getUpdateTime());
                vo.setPay(true);
            }
            resultDtos.add(vo);
        }
        return success(resultDtos);
    }

    /**
     * 导出缴费项目列表
     */
    @PreAuthorize("@ss.hasPermi('campus:activity:export')")
    @Log(title = "缴费项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CampusActivity campusActivity) {
        List<CampusActivity> list = campusActivityService.selectCampusActivityList(campusActivity);
        ExcelUtil<CampusActivity> util = new ExcelUtil<>(CampusActivity.class);
        util.exportExcel(response, list, "缴费项目数据");
    }

    /**
     * 获取缴费项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('campus:activity:query')")
    @GetMapping(value = "/{actId}")
    public AjaxResult getInfo(@PathVariable("actId") Long actId) {
        return success(campusActivityService.selectCampusActivityByActId(actId));
    }

    /**
     * 新增缴费项目
     */
    @PreAuthorize("@ss.hasPermi('campus:activity:add')")
    @Log(title = "缴费项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CampusActivity campusActivity) {
        return toAjax(campusActivityService.insertCampusActivity(campusActivity));
    }

    /**
     * 修改缴费项目
     */
    @PreAuthorize("@ss.hasPermi('campus:activity:edit')")
    @Log(title = "缴费项目", businessType = BusinessType.UPDATE)
    @PostMapping("/put")
    public AjaxResult edit(@RequestBody CampusActivity campusActivity) {
        return toAjax(campusActivityService.updateCampusActivity(campusActivity));
    }

    /**
     * 删除缴费项目
     */
    @PreAuthorize("@ss.hasPermi('campus:activity:remove')")
    @Log(title = "缴费项目", businessType = BusinessType.DELETE)
    @PostMapping("/{actIds}/delete")
    public AjaxResult remove(@PathVariable Long[] actIds) {
        return toAjax(campusActivityService.deleteCampusActivityByActIds(actIds));
    }
}
